home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2029 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.0 KB

  1. Path: solon.com!not-for-mail
  2. From: seebs@solutions.solon.com (Peter Seebach)
  3. Newsgroups: comp.lang.c,comp.lang.c++,comp.lang.perl
  4. Subject: Re: Stupid array problems
  5. Date: 14 Jan 1996 21:54:44 -0600
  6. Organization: Usenet Fact Police (Undercover)
  7. Message-ID: <4dcj64$ome@solutions.solon.com>
  8. References: <4d9b9v$14n@paperboy.ids.net> <4dbfd1$2hpc@news.gate.net> <4dc5fv$qdr@newsreader.wustl.edu>
  9. NNTP-Posting-Host: solutions.solon.com
  10.  
  11. In article <4dc5fv$qdr@newsreader.wustl.edu>,
  12. Krishnamoorthy Lakshminarayan <kln@howdy.wustl.edu> wrote:
  13. >    void delete_array_element (char **array,  //The array of strings
  14. >                                   int  max,      // total strings in array
  15. >                                   int  num_del)  //index you want deleted
  16. >      {
  17. >      for (int i=max-1; i>num_del; i++) //max-1, because you've deleted
  18. >                                            //one element
  19. >        {
  20. >        array[i] = array[i-1];
  21. >        }
  22. >      }
  23.  
  24. HUH?
  25.  
  26. 1.  This is clearly C++, and the question is in comp.lang.c and
  27. comp.lang.perl.
  28. 2.  The reason i would start at max - 1 is not that you've deleted one
  29. element, but because the top element in a 10 element array is array[9].
  30. 3.  Let's try your code on a trivial case: A 3 element array, delete
  31. the middle one.  So...
  32.     x = { 1, 2, 3 };
  33.     delete(x, 3, 1)
  34.         i = 2
  35.         x[2] = x[1];
  36.         inc i -> i = 3
  37.         ...
  38. Hmm.  Looks like you're going backwards.  Lets count the other way...
  39.         after x[2] = x[1];
  40.         i = 1;
  41.         break (i no longer < num_del)
  42.     ...
  43.     We end up with
  44.     x = { 1, 2, 2 };
  45. ... Looks like this *copies* the element.
  46.  
  47. Please check solutions before posting them, and use a language appropriate
  48. to at least one of the groups.  C and C++ are *NOT* the same language, and
  49. frequently have vastly different semantics for seemingly identical things.
  50.  
  51. -s
  52. -- 
  53. Peter Seebach - seebs@solon.com - Copyright 1995 Peter Seebach.
  54. C/Unix proto-wizard -- C/Unix questions? Send mail for help.  No, really!
  55. Using trn?  Weird new newsgroup problem?  I know the fix!  Email me!
  56. The *other* C FAQ - ftp taniemarie.solon.com /pub/c/afq - Not A Flying Toy
  57.